package com.fcg.redis.topone.controller;

import com.fcg.redis.rediscommon.bean.ResponseBean;
import com.fcg.redis.topone.bean.Goods;
import com.fcg.redis.topone.service.TopOneService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.temporal.ChronoUnit;

/**
 * @author FCG
 */
@RestController
public class GoodsController {

    @Resource
    private TopOneService topOneService;

    @PostMapping("buy")
    public ResponseBean buyGoods(@RequestBody @Validated(Goods.BuyGoods.class) Goods goods) {
        return new ResponseBean(topOneService.buy(goods.getId()));
    }

    @GetMapping("topOne/{scope}/{topCount}")
    public ResponseBean getTopOne(@PathVariable int scope, @PathVariable int topCount) {
        ChronoUnit unit;
        switch (scope) {
            case 0:
                unit = ChronoUnit.FOREVER;
                break;
            case 1:
                unit = ChronoUnit.YEARS;
                break;
            case 2:
                unit = ChronoUnit.MONTHS;
                break;
            default:
                throw new IllegalArgumentException("scope can't be: " + scope);
        }
        return new ResponseBean(topOneService.topOne(unit, topCount));
    }
}
















